package com.devin.controller;

import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
public class MyErrorController implements ErrorController {

    private final String ERROR_PATH = "/error";

    @Override
    public String getErrorPath() {
        return ERROR_PATH;
    }

    /*MediaType.TEXT_HTML_VALUE 是按照什么处理，此处为 text/html*/
    @RequestMapping(value = ERROR_PATH,produces = MediaType.TEXT_HTML_VALUE)
    public String errorPageHandler(HttpServletRequest request, HttpServletResponse response){
        Integer statusCode = (Integer)request.getAttribute("javax.servlet.error.status_code");
        System.out.println(statusCode);
//        int status = response.getStatus();
//        System.out.println(status);
        if(statusCode==null){
            statusCode=500;

        }
        switch (statusCode){
            case 403:
                System.out.println("===controller==》403 ");
                return "error/403";
            case 404:
                System.out.println("===controller==》404 ");
                return "error/404";
            case 500:
                System.out.println("===controller==》500 ");
                return "error/500";
            case 666:
                /*登录页认证失败，一般在表单验证就解决了，不会在此解决*/
                System.out.println("====controller==>>认证失败");
                return  "error/666";
            default:
                System.out.println("===controller==》other ");
                return "error/other";

        }
    }

}
